package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.SplashActivity;
import com.warmlight.voicepacket.data.eventbus.StopFloatService;
import com.warmlight.voicepacket.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by jason on 2018/5/25.
 */

public class FloatSetting extends RelativeLayout {

    private Context mContext;
    private RelativeLayout mContainer;
    private SeekBar sb_delay_time;
    private TextView tv_audio_delay_time;

    public FloatSetting(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public FloatSetting(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public FloatSetting(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FloatSetting(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    private void init(){
        mContainer = (RelativeLayout) View.inflate(mContext, R.layout.float_setting_content, this);
        tv_audio_delay_time = (TextView) mContainer.findViewById(R.id.tv_audio_delay_time);
        sb_delay_time = (SeekBar) mContainer.findViewById(R.id.sb_delay_time);
        sb_delay_time.setMax(10);
        sb_delay_time.setProgress((int)SPUtils.getFloatVoiceDelayTime() / 1000);
        tv_audio_delay_time.setText((int)SPUtils.getFloatVoiceDelayTime() / 1000 + "s");
        sb_delay_time.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                tv_audio_delay_time.setText(seekBar.getProgress()+"s");
                SPUtils.setFloatVoiceDelayTime(seekBar.getProgress() * 1000L);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        mContainer.findViewById(R.id.iv_float_swich).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new StopFloatService("stopFloatSelf"));
            }
        });

        mContainer.findViewById(R.id.tv_enter_app).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SplashActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
    }
}
